<?php

class Listener_Finance_Order extends Listener_Base
{
    public function handle($args)
    {
        $self = new self();
        $self->args = $args;
        //判断队列是否开启,如果开启则把任务推送到队列
        if ($self->queue_status) {
            Queue::in('default', Listener_Finance_Order::class, $args);
        } else {
            $self->perform();
        }
    }

    public function perform()
    {
        $order = $this->args['order'] ?? $this->args;
        $event_name = St_Functions::get_event_name($order['status']);
        St_Log::write('财务数据记录', array('status' => $order['status']), "event/{$event_name}/log");
        switch ($order['status']) {
            case 0:
            case 1:
                $res = Model_Finance_Listener::order_set_count_info($order['ordersn']);
                break;
            case 2:
            case 5:
                $res = Model_Finance_Listener::platform_income($order['ordersn']);
                break;
            default:
                $res = [
                    'status' => false,
                    'msg'    => '订单状态错误，仅处理状态0、1、2、5的订单'
                ];
                break;
        }
        St_Log::write('财务数据记录', array('status' => $order['status'], 'ordersn' => $order['ordersn'], 'result' => $res), "event/{$event_name}/log");
    }
}